package vashion.azeroth.core.service.impl;

import java.util.ArrayList;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import vashion.azeroth.contant.ExperienceErrorEnum;
import vashion.azeroth.core.manager.personal.PersonalExperienceManager;
import vashion.azeroth.core.manager.personal.PersonalFriendManager;
import vashion.azeroth.core.manager.personal.PersonalMemberManager;
import vashion.azeroth.core.personal.pojo.PersonalExperience;
import vashion.azeroth.core.personal.pojo.PersonalMember;
import vashion.azeroth.core.result.ApiResult;
import vashion.azeroth.core.service.OperateExperienceService;
import vashion.azeroth.core.service.PersonalConnectionService;
import vashion.azeroth.core.service.PersonalExperienceService;

@Service
public class PersonalExperienceServiceImpl<T> implements
		PersonalExperienceService<T> {

	private final static Logger log = LoggerFactory
			.getLogger(PersonalExperienceServiceImpl.class);

	@Autowired
	PersonalMemberManager personalMemberManager;

	@Autowired
	PersonalExperienceManager personalExperienceManager;

	@Autowired
	OperateExperienceService<T> operateExperienceService;

	@Autowired
	private PersonalFriendManager personalFriendManager;

	@Autowired
	private PersonalConnectionService<T> connectionService;

	@Override
	public ApiResult<Integer> getExperienceByMemberId(
			PersonalExperience experience) {
		int exp_value = 0;
		ApiResult<Integer> exp_result = new ApiResult<Integer>();
		try {
			PersonalExperience experienceRes = personalExperienceManager
					.getExperienceByMemberId(experience);
			if (null == experienceRes
					|| StringUtils
							.isBlank(String.valueOf(experience.getValue()))) {
				return ApiResult.newErrorResult(
						ExperienceErrorEnum.NO_EXP.getErrorCode(),
						ExperienceErrorEnum.NO_EXP.getErrorMessage());
			}
			exp_value = experienceRes.getValue();
			exp_result = ApiResult.newSuccessResult(Integer.valueOf(exp_value));

		} catch (Exception e) {
			log.error("获取用户经验值异常" + e);
		}
		return exp_result;
	}

	@Override
	public ApiResult<Integer> getExperience(PersonalExperience experience) {
		return null;
	}

	@Override
	public ApiResult<Double> updateExperienceMemberId(
			PersonalExperience experience) {
		int exp_base = 0;
		ApiResult<Double> exp_result = new ApiResult<Double>();

		try {
			PersonalExperience experienceRes = personalExperienceManager
					.getExperienceByMemberId(experience);
			if (null == experienceRes
					|| StringUtils
							.isBlank(String.valueOf(experience.getValue()))) {
				return ApiResult.newErrorResult(
						ExperienceErrorEnum.NO_EXP.getErrorCode(),
						ExperienceErrorEnum.NO_EXP.getErrorMessage());
			}
			// 获取base经验值
			exp_base = experienceRes.getValueBase();
			// 获取上、下线会员信息
			PersonalMember member = new PersonalMember();
			member.setMemberId(experience.getMemberId());
			PersonalMember[] lowers = null;
			PersonalMember higher = null;

			try {
				// 用户的上线、下线为空的情况在计算经验值的时候会处理
				// 获取上线用户
				PersonalMember curMember = personalMemberManager.get(member);
				higher = personalMemberManager.get(curMember
						.getInviteMemberId());

				// 获取下线用户
				ArrayList<PersonalMember> resMemberList = (ArrayList<PersonalMember>) personalMemberManager
						.findLowererByInviteMemberId(member);
				lowers = new PersonalMember[resMemberList.size()];
				for (int i = 0; i < resMemberList.size(); i++) {
					PersonalMember lowerMember = personalMemberManager
							.get(resMemberList.get(i).getMemberId());
					lowers[i] = lowerMember;
				}
			} catch (Exception e) {
				log.error("计算上、下线的加权经验值失败" + e);
			}

			// 计算实际经验值
			double resExp = operateExperienceService.getOperateMyExperience(
					higher, lowers, exp_base);
			exp_result = ApiResult.newSuccessResult(resExp);
		} catch (Exception e) {
			log.error("计算经验值异常" + e);
		}
		return exp_result;
	}

	@Override
	public ApiResult<Integer> updateExperienceBase(PersonalExperience experience) {
		ApiResult<Integer> exp_result = new ApiResult<Integer>();
		PersonalExperience res_exp = new PersonalExperience();

		try {
			PersonalExperience experienceRes = personalExperienceManager
					.getExperienceByMemberId(experience);
			if (null == experienceRes
					|| StringUtils
							.isBlank(String.valueOf(experience.getValue()))) {
				res_exp = personalExperienceManager.save(experience);

				return ApiResult.newSuccessResult(Integer.valueOf(res_exp
						.getValueBase()));
			}

			Integer cur_base = experienceRes.getValueBase();
			Integer newValBase = experience.getValueBase() + cur_base;
			if (newValBase < 0) {
				newValBase = 0;
			}
			experience.setValueBase(newValBase);
			int res_upd = personalExperienceManager
					.updateByMemberId(experience);
			if (Integer.valueOf(res_upd) == 1) {
				PersonalExperience upd_base = personalExperienceManager
						.getExperienceByMemberId(experience);
				exp_result = ApiResult
						.newSuccessResult(upd_base.getValueBase());
			} else {
				log.error("经验值更新失败");
			}
		} catch (Exception e) {
			log.error("计算经验值异常" + e);
		}
		return exp_result;
	}

}
